//
//

package sqs

import (
	"encoding/xml"
	"net/http"
	"strconv"
)

type xmlErrors struct {
	Errors []Error `xml:"Error"`
}

type Error struct {
	StatusCode int
	Type       string
	Code       string
	Message    string
}

func buildError(r *http.Response) error {
	var (
		err  Error
		errs xmlErrors
	)
	xml.NewDecoder(r.Body).Decode(&errs)
	if len(errs.Errors) > 0 {
		err = errs.Errors[0]
	}
	err.StatusCode = r.StatusCode
	if err.Message == "" {
		err.Message = r.Status
	}
	return &err
}

func (e *Error) Error() string {
	var prefix string
	if e.Code != "" {
		prefix = e.Code + ": "
	}
	if prefix == "" && e.StatusCode > 0 {
		prefix = strconv.Itoa(e.StatusCode) + ": "
	}
	return prefix + e.Message
}

// vim: fdm=syntax fdn=1
